package cn.dnui_cr503.dnuifood_16110100503.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.adapter.CommentsAdapter;

import cn.dnui_cr503.dnuifood_16110100503.bean.CommentsBean;

import cn.dnui_cr503.dnuifood_16110100503.bean.FoodBean;

import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.model.CollectionModel;
import cn.dnui_cr503.dnuifood_16110100503.model.FoodModel;
import cn.dnui_cr503.dnuifood_16110100503.model.UserModel;

public class FoodActivity extends AppCompatActivity implements OnRetrofitListener {
    private Context context;
    private String food_id;
    private TextView food_name, food_price, food_inro;
    private ImageView food_img;
    private FoodBean foodInfo;
    private List<CommentsBean> commentsInfo;
    private RecyclerView recyclerView;
    private CommentsAdapter commentsAdapter;
    private Button button_buy;
    private Menu menu;

    public FoodActivity() {
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);

        food_id = getIntent().getStringExtra("food_id");

        initViews();
        initData(food_id);
    }

    void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.comments_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        food_name = (TextView) findViewById(R.id.food_name);
        food_inro = (TextView) findViewById(R.id.food_inro);
        food_price = (TextView) findViewById(R.id.food_price);
        food_img = (ImageView) findViewById(R.id.food_img);

        button_buy = (Button) findViewById(R.id.button_buy);
    }

    private void initEvents() {

    }

    void initData(String food_id) {
        showFoodInfo(food_id);
        showComments(food_id);
    }

    private void showFoodInfo(String food_id) {
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodById(food_id, this);//访问服务器请求
    }

    private void showComments(String food_id) {
        FoodModel foodModel = new FoodModel();
        foodModel.getAllCommentsByFood(food_id, this);
    }

    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag) {
            case 2:
                this.foodInfo = (FoodBean) object;
                food_name.setText(foodInfo.getFoodname());
                food_price.setText(foodInfo.getPrice() + "元");
                food_inro.setText(foodInfo.getIntro());
                Picasso.with(context).load(UserModel.Constants.BASE_URL + foodInfo.getPic()).into(food_img);
                button_buy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(FoodActivity.this, BuyActivity.class);
                        intent.putExtra("shop_id", foodInfo.getShop_id());
                        intent.putExtra("food_id", foodInfo.getFood_id());
                        intent.putExtra("food_name", foodInfo.getFoodname());
                        intent.putExtra("price", foodInfo.getPrice());
                        startActivity(intent);
                    }
                });
                break;
            case 3:
                commentsInfo = (List<CommentsBean>) object;
                commentsAdapter = new CommentsAdapter(FoodActivity.this, commentsInfo);
                recyclerView.setAdapter(commentsAdapter);
                break;
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(FoodActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.menu_collect,menu);
        this.menu=menu;
        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
        isCollected(menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //实现"显示收藏还是取消收藏"
        switch (item.getItemId()){
            case R.id.item_collect:
                String title = item.getTitle().toString();
                if (title.equals("收藏"))
                {
                    //收藏
                    collectFood(menu);
                }
                else
                {
                    //取消收藏
                    uncollectFood(menu);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void isCollected(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectionModel collectModel = new CollectionModel();
        collectModel.isCollect(user_id,food_id,"1",menu);
    }

    private void collectFood(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectionModel collectModel = new CollectionModel();
        collectModel.userCollectFood(user_id,food_id,menu);

    }

    private void uncollectFood(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectionModel collectModel = new CollectionModel();
        collectModel.unCollectFood(user_id,food_id,menu);
    }
}






